package org.springboot3.project.web.controller;

import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springboot3.project.web.domain.SysUser;
import org.springboot3.project.web.service.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * todo
 *
 * @author weiwei
 * @since 2024/6/11 11:38
 */
@Slf4j
@Tag(name = "测试接口", description = "功能测试")
@RestController
public class TestController {

    @Autowired
    @Lazy
    private ConfigService nacosConfigService;

    @Autowired
    private SysUserServiceImpl sysUserService;

    @Operation(summary = "测试hello", description = "测试sayHello")
//    @Parameters({
//            @Parameter(name = "name", description = "用户名", required = false, in = ParameterIn.QUERY)
//    })
    @GetMapping("/sayHello")
    public String sayHello(@RequestParam("name") @Parameter(name = "name", description = "用户名") String name) {
        return "Hello " + name;
    }

    @Operation(description = "获取nacos动态配置数据")
    @GetMapping("/getNacosConfigTest")
    public String getNacosConfigTest() {
        String dataId = "test";
        String group = "test";
        try {
            var config = nacosConfigService.getConfig(dataId, group, 5000);
            log.info("getNacosConfigTest config:{}", config);
            return config;
        } catch (NacosException e) {
            return "NacosException: " + e.getMessage();
        }
    }

    @Operation(description = "推送数据到nacos")
    @Parameters({
            @Parameter(name = "content", description = "配置内容", required = true, in = ParameterIn.QUERY)
    })
    @GetMapping("/publishNacosConfigTest")
    public String publishNacosConfigTest(@RequestParam("content")  String content) {
        String dataId = "test";
        String group = "test";
        try {
            boolean isSuccess = nacosConfigService.publishConfig(dataId, group, content);
            if (isSuccess) {
                log.info("nacos config update success!");
                // nacos更新配置有一点点延时，不加延时可能获取到的是上一次的结果
                Thread.sleep(200L);
                return nacosConfigService.getConfig(dataId, group, 5000);
            }else {
                return "failed";
            }
        } catch (Exception e) {
            return "NacosException: " + e.getMessage();
        }
    }

    @Operation(description = "测试数据库插入")
    @GetMapping("/insert")
    public void print() {
        SysUser sysUser = new SysUser();
        sysUser.setUserId(1L);
        sysUser.setUserName("张三");
        sysUser.setNickName("三子");
        sysUser.setUserType("sys");
        sysUser.setRemark("他是张三");
        sysUserService.save(sysUser);
    }

    @Operation(description = "测试数据库更新")
    @GetMapping("/update")
    public void update() {
        SysUser sysUser = new SysUser();
        sysUser.setUserId(1L);
        sysUser.setUserName("张三");
        sysUser.setNickName("三子");
        sysUser.setUserType("sys_user");
        sysUser.setRemark(null);
        sysUserService.updateById(sysUser);
    }

}
